package com.ubhave.powertracker;

import java.util.Date;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class PageLogger{
	SharedPreferences settings;
	SharedPreferences.Editor editor;
	
	LifeGuideConnector lgc;
	String usr;
	String intervention;
	
	Context context;
	
	String additional;
	
	String prevLog;
	int logCounter;
	
	public PageLogger (Context c){
		
		context = c;
		
		settings=PreferenceManager.getDefaultSharedPreferences(PowerTrackerApplication.getContext());
		editor = settings.edit();
		
		lgc=new LifeGuideConnector(settings);    	
        usr = settings.getString("LifeGuideUser", "");
		intervention = settings.getString("LifeGuideInterventionId", "");
		
		logCounter=settings.getInt("logCounter", 0);
		
		constructLog();
	}
	
	public PageLogger (Context c, String a){
		
		context = c;
		additional = a;
		
		settings=PreferenceManager.getDefaultSharedPreferences(PowerTrackerApplication.getContext());
		editor = settings.edit();
		
		lgc=new LifeGuideConnector(settings);    	
        usr = settings.getString("LifeGuideUser", "");
		intervention = settings.getString("LifeGuideInterventionId", "");
		
		logCounter=settings.getInt("logCounter", 0);
		
		constructLog();
	}
	
	public void constructLog(){
		prevLog = settings.getString("usageLog"+logCounter, "");
		if(prevLog.length()>10000){
			logCounter++;
			editor.putInt("logCounter",logCounter);		
	        editor.commit();
		}		
		String log = settings.getString("usageLog"+logCounter, "")+context.getClass().getSimpleName();
		if(additional!=null)
			log+="("+additional+")";
		log+=" "+new Date()+", ";
		editor.putString("usageLog"+logCounter,log);		
        editor.commit();		
	}
	
	public void makeLog() throws PowerTrackerInvalidAuthKeyException{
		
		if(!usr.equals("")&&!intervention.equals("")){
			String serverLog = lgc.getVar(intervention, usr, "user", "appUsageLog"+logCounter,settings.getString("authkey",""));
			if(serverLog!=null&&!serverLog.equals("")&&(prevLog.length()<serverLog.length()||(prevLog.length()>serverLog.length()&&!prevLog.substring(0, serverLog.length()).equals(serverLog)))){
				editor.putString("usageLog"+logCounter,serverLog);		
		        editor.commit();
		        constructLog();
		        makeLog();
			}
			else
				lgc.postVar(intervention, usr, "user", "appUsageLog"+logCounter, settings.getString("usageLog"+logCounter, ""),settings.getString("authkey",""));
		}
        else
        	Log.i("POWeR Tracker: ", "Log fail usr: "+usr+" int: "+intervention);
	}
	
	/*public void makeLog() throws PowerTrackerInvalidAuthKeyException{
		
		String prevLog = settings.getString("usageLog", "");
		String log = settings.getString("usageLog", "")+context.getClass().getSimpleName();
		if(additional!=null)
			log+="("+additional+")";
		log+=" "+new Date()+", ";
		editor.putString("usageLog",log);		
        editor.commit();
		if(!usr.equals("")&&!intervention.equals("")){
			String serverLog = lgc.getVar(intervention, usr, "user", "appUsageLog",settings.getString("authkey",""));
			if(serverLog!=null&&!serverLog.equals("")&&(prevLog.length()<serverLog.length()||(prevLog.length()>serverLog.length()&&!prevLog.substring(0, serverLog.length()).equals(serverLog)))){
				editor.putString("usageLog",serverLog);		
		        editor.commit();
		        makeLog();
			}
			else
				lgc.postVar(intervention, usr, "user", "appUsageLog", settings.getString("usageLog", ""),settings.getString("authkey",""));
		}
        else
        	Log.i("POWeR Tracker: ", "Log fail usr: "+usr+" int: "+intervention);
	}*/
	
}